package com.cskaoyan.market.controller.wx;

import com.cskaoyan.market.bo.WxCommentBo;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.WxCommentService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxCommentVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/comment/")
public class WxCommentController {
    @Autowired
    WxCommentService wxCommentService;
    @Autowired
    SecurityManager securityManager;

    @GetMapping("count")
    public Object count(Integer valueId, byte type) {
        Map<String, Object> data = wxCommentService.count(valueId, type);
        Object ok = ResponseUtil.ok(data);
        return ok;
    }

    @GetMapping("list")
    public Object list(Integer valueId, byte type, Integer limit, Integer page, Integer showType) {
        List<WxCommentVo> list = wxCommentService.list(valueId, type, limit, page, showType);
        Object o = ResponseUtil.okList(list);
        return o;
    }
    @PostMapping("post")
    public Object post(@RequestBody WxCommentBo wxCommentBo){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        MarketComment result=wxCommentService.post(wxCommentBo,user);
        Object ok = ResponseUtil.ok(result);
        return ok;
    }
}
